package com.yunhai.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.enums.LanuageEnum;
import com.yunhai.common.utils.StringUtils;
import com.yunhai.system.domain.Banner;
import com.yunhai.system.service.BannerService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/banner")
public class BannerController extends BaseController {
    @Autowired
    private BannerService bannerService;

    /**
     * 轮播图
     * @param param
     * @return
     */
    @PostMapping("list")
    public AjaxResult getBanner(HttpServletRequest httpServletRequest, @RequestBody Map<String, Object> param) {
        Integer position = MapUtils.getInteger(param, "position");
        if (null == position) {
            return AjaxResult.error("位置不能为空");
        }
        String language = httpServletRequest.getHeader("yh-language");
        if (StringUtils.isBlank(language)) {
            language = LanuageEnum.CH.getCode().toString();
        }
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.eq("position", position);
        bannerQueryWrapper.eq("language", language);
        bannerQueryWrapper.orderByAsc("sort_num");
        return AjaxResult.success(bannerService.list(bannerQueryWrapper));
    }

}
